package org.wms.minbearwcs.serivce.agv.task.agvTask.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.wms.minbearwcs.dao.agv.inf.AgvMissionDao;
import org.wms.minbearwcs.dao.agv.inf.AgvMissionDetailDao;
import org.wms.minbearwcs.entity.DTO.agv.TUSK.TUSKContentPool;
import org.wms.minbearwcs.entity.DTO.agv.TUSK.TUSKMission;
import org.wms.minbearwcs.entity.DTO.agv.TUSK.TUSKResponse;
import org.wms.minbearwcs.entity.DTO.agv.task.AgvActionSource;
import org.wms.minbearwcs.entity.DTO.agv.task.AgvTaskSource;
import org.wms.minbearwcs.entity.PO.agv.AgvMission;
import org.wms.minbearwcs.entity.PO.agv.AgvMissionDetail;
import org.wms.minbearwcs.entity.PO.agv.missionEnum.AgvMissionStatus;
import org.wms.minbearwcs.serivce.agv.task.agvTask.AgvTaskDispatcher;
import org.wms.minbearwcs.utils.IdWorker;
import org.wms.minbearwcs.utils.TUSKHpptUtil;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
public class AgvTaskDispatcherImpl implements AgvTaskDispatcher {
    
    private final IdWorker idWorker;
    private final AgvMissionDao agvMissionDao;
    private final AgvMissionDetailDao agvMissionDetailDao;
    private final TUSKHpptUtil tuskHpptUtil;
    
    @Override
    public boolean dispatch(AgvTaskSource agvTaskSource) {
        String taskId = "Task_Agv_" + idWorker.nextId();
        String targetCollect = agvTaskSource.getActionList()
                .stream()
                .map(AgvActionSource::getPoint)
                .collect(Collectors.joining(",", "[", "]"));

        AgvMission agvMission = new AgvMission(null, taskId, agvTaskSource.getMissionType(), AgvMissionStatus.READY,
                1, targetCollect, agvTaskSource.getActionList().size(), 0, null, agvTaskSource.getWcsMissionId(),
                agvTaskSource.getWcsMissionType(), agvTaskSource.getWcsMissionStatus(), LocalDateTime.now(),
                null, null);
        agvMissionDao.insert(agvMission);

        AtomicInteger actionIndexCount = new AtomicInteger(1);
        agvTaskSource.getActionList()
                .stream()
                .map(a-> new AgvMissionDetail(null,taskId, actionIndexCount.getAndIncrement(), a.getPoint(),
                            a.getAction().name(),null,0))
                .collect(Collectors.toList())
                .forEach(agvMissionDetailDao::insert);

        List<String> pointList = agvTaskSource.getActionList()
                .stream()
                .map(AgvActionSource::getPoint)
                .collect(Collectors.toList());
        TUSKMission tuskMission = new TUSKMission(taskId, pointList, agvTaskSource.getMissionType().name());
        TUSKResponse tuskResponse = tuskHpptUtil.dispatchTUSKMission(tuskMission);


        return Objects.equals(TUSKContentPool.TUSK_SUCCESS_CODE, tuskResponse.getCode());
    }
}
